package com.jiang.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.bind.validation.ValidationErrors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Api("Swagger相关接口")
@RestController
@Slf4j
public class SwaggerController {

    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    static class DemoParam {
        @NotNull(message = "id不能为空")
        private Long id;
        @NotBlank(message = "name不能为空")
        private String name;
        private String desc;
    }

    @ApiOperation(value = "验证参数增强接口")
    @GetMapping("/swagger")
    public String swagger(@RequestBody @Validated DemoParam param, ValidationErrors errors) {
        if (errors.hasErrors()) {
            return errors.getAllErrors().toString();
        }
        return "swagger";
    }

}
